<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\AdminController;
use App\Model\Menu;


class MenuController extends AdminController
{
    /**
     * 菜单列表
     * @author yhs 2019.07.30
     * @return list
     */
    public function getmenu(Request $request){
        $limit = $request->limit;
        $list = Menu::where('parent_id',0)->paginate($limit);
        return $list;
    }
    /**
     * 菜单添加和修改
     * @author yhs 2019.07.30
     * @return list
     */
    public function menuadd(Request $request){
        if(empty($request->menu_name) || empty($request->eg_name)){
            $back['message'] = "菜单名称不能为空！";
            $back['status'] = 0;
            return $back;
        }
        $data = [
            'menu_name' => $request->menu_name,
            'eg_name' => $request->eg_name,
            'controller' => $request->controller,
            'method' => $request->method,
            'icon' => $request->icon,
        ];
        try{
            if(empty($request->id)){
                $data['created_at'] = date('Y-m-d H:i:s');
                Menu::insert($data);
                $back['message'] = "添加成功！";
                $back['status'] = 1;
            }else{
                Menu::where('id',$request->id)->update($data);
                $back['message'] = "修改成功！";
                $back['status'] = 1;
            }
        }catch(\Execption $e){
            $back['message'] = $e->getMessage();
            $back['status'] = 0;
        }
        return $back;
    }
    /**
     * 修改菜单页面
     * @author yhs 2019.07.30
     * @return array
     */
    public function menuedit(Request $request){
        if(!empty($request->id)){
            $find = Menu::where('id',$request->id)->first();
        }else{
            $find = [];
        }
        return $find;
    }
    /**
     * 菜单状态
     * @author yhs 2019.07.30
     * @return array
     */
    public function menuValid(Request $request){
        if(!empty($request->id)){
            if($request->valid == 1){
                $valid = 0;
            }elseif($request->valid == 0){
                $valid = 1;
            }
            Menu::where('id',$request->id)->update(['valid'=>$valid]);
            $back['message'] = '修改成功';
            $back['valid'] = $valid;
            $back['status'] = 1;
        }else{
            $back['message'] = '参数传递有误！';
            $back['status'] = 0;
        }
        return $back;
    }
    /**
     * 当前子菜单列表
     * @author yhs 2019.07.30
     * @return array
     */
    public function getSubMenu(Request $request){
        $find = Menu::where('id',$request->parent_id)->first();
        $limit = $request->limit;
        if(!empty($request->parent_id)){
            $list = Menu::where('parent_id',$request->parent_id)->paginate($limit);
        }else{
            $list = Menu::where('parent_id','!=',0)->paginate($limit);
        }
        $data = [
            'list'=>$list,
            'find'=>$find,
        ];
        return $data;
    }
    /**
     * 子菜单添加和修改
     * @author yhs 2019.07.30
     * @return list
     */
    public function subMenuAdd(Request $request){
        if(empty($request->parent_id)){
            $back['message'] = "父级菜单不能为空！";
            $back['status'] = 0;
            return $back;
        }
        if(empty($request->menu_name) || empty($request->eg_name)){
            $back['message'] = "菜单名称不能为空！";
            $back['status'] = 0;
            return $back;
        }
        $data = [
            'parent_id' => $request->parent_id,
            'menu_name' => $request->menu_name,
            'eg_name' => $request->eg_name,
            'controller' => $request->controller,
            'method' => $request->method,
        ];
        try{
            if(empty($request->id)){
                $data['created_at'] = date('Y-m-d H:i:s');
                Menu::insert($data);
                $back['message'] = "添加成功！";
                $back['status'] = 1;
            }else{
                Menu::where('id',$request->id)->update($data);
                $back['message'] = "修改成功！";
                $back['status'] = 1;
            }
        }catch(\Execption $e){
            $back['message'] = $e->getMessage();
            $back['status'] = 0;
        }
        return $back;
    }
    /**
     * 修改子菜单页面
     * @author yhs 2019.07.30
     * @return array
     */
    public function subEenuEdit(Request $request){
        if(!empty($request->id)){
            $find = Menu::where('id',$request->id)->first();
        }else{
            $find = [];
        }
        return $find;
    }

    
}
